import React, { useEffect, useState } from 'react';
import { useAuthStore } from '../stores/authStore';

interface SimplifiedAuthProviderProps {
  children: React.ReactNode;
}

// 简化的认证提供者，避免复杂的依赖关系
const SimplifiedAuthProvider: React.FC<SimplifiedAuthProviderProps> = ({ children }) => {
  const [isInitialized, setIsInitialized] = useState(false);
  const { isLoading, isAuthenticated, initializeAuth } = useAuthStore();

  // 只在组件挂载时初始化一次
  useEffect(() => {
    if (!isInitialized) {
      console.log('SimplifiedAuthProvider: 初始化认证状态');
      setIsInitialized(true);
      initializeAuth().finally(() => {
        // 确保初始化完成后设置状态
        console.log('SimplifiedAuthProvider: 认证状态初始化完成');
      });
    }
  }, [isInitialized, initializeAuth]);

  // 如果还在初始化，显示加载状态
  if (isLoading && !isAuthenticated) {
    return React.createElement('div', {
      style: {
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center',
        height: '100vh',
        backgroundColor: '#000',
        color: '#fff'
      }
    }, '初始化认证...');
  }

  return React.createElement(React.Fragment, null, children);
};

export default SimplifiedAuthProvider;